﻿using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
using CommunityToolkit.Mvvm.Messaging;
using HandyControl.Expression.Shapes;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Drawing;
using System.Globalization;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using WpfDemo.Common;
using WpfDemo.IRepository.Sys;

namespace WpfDemo.ViewModel
{
    public partial class MainWindowViewModel: ObservableObject
    {

        IModuleRepository _repository;
        public MainWindowViewModel(IModuleRepository repository)
        {
            _repository = repository;
            ModuleGroups = new ObservableCollection<ModuleGroup>();
            TabModels = new ObservableCollection<TabModel>();
            Modules = new ObservableCollection<Modules>();
            Modules modulesModel = new Modules
            {
                Code = "\ue600",
                Name = "用户管理",
                TypeName = "system.user.User"
            };
            Modules.Add(modulesModel);
            tabIndex = 0;

            //ExpandMenuCmd();
            getMenu();
            changeContentCmd("home");
        }
        #region =====data

        /// <summary>
        /// 已加载模块-分组
        /// </summary>
        [ObservableProperty]
        private ObservableCollection<ModuleGroup> moduleGroups;

        /// <summary>
        /// 菜单
        /// </summary>
        [ObservableProperty]
        private ObservableCollection<MenuItemModel> menus;

        /// <summary>
        /// 已点击模块
        /// </summary>
        [ObservableProperty]
        private ObservableCollection<TabModel> tabModels;

        /// <summary>
        /// 已加载模块
        /// </summary>
        [ObservableProperty]
        private ObservableCollection<Modules> modules;

        [ObservableProperty]
        private int tabIndex;

        /// <summary>
        /// 模块加载
        /// </summary>
        [ObservableProperty]
        private FrameworkElement mainContent;


        /// <summary>
        /// 
        /// </summary>
        [ObservableProperty]
        public ObservableCollection<UserModel> gridModelList;


        #endregion


        [RelayCommand]
        private void ExpandMenuCmd()
        {
            for (int i = 0; i < ModuleGroups.Count; i++)
            {
                var item = ModuleGroups[i];
                item.ContractionTemplate = !item.ContractionTemplate;
            }
            WeakReferenceMessenger.Default.Send<string, string>("", "ExpandMenuToken");
        }
        /// <summary>
        /// [RelayCommand]
        /// </summary>
        /// <param name="o"></param>
        [RelayCommand]
        private void changeContentCmd(object o)
        {
            string typeName;
            string tabName;
            if (o.ToString() == "home")
            {
                typeName = "home";
                tabName = "首页";
            }
            else
            {
                var values = (object[])o;
                typeName = values[0].ToString();
                tabName = values[1].ToString();
            }

            Type type = Type.GetType("WpfDemo.View." + typeName);
            ConstructorInfo constructor = type.GetConstructor(Type.EmptyTypes);
            bool needAdd = true;
            var tempCode = string.Empty;
            for (int i = 0; i < TabModels.Count; i++)
            {
                tempCode = string.Empty;
                if (o.GetType().IsArray)
                {
                    var values = (object[])o;
                    tempCode = values[0].ToString();
                }
                else
                    tempCode = o.ToString();
                if (TabModels[i].Code == tempCode)
                {
                    TabIndex = i;
                    needAdd = false;
                    break;
                }
            }
            if (needAdd)
            {
                tempCode = string.Empty;
                if (o.GetType().IsArray)
                {
                    var values = (object[])o;
                    tempCode = values[0].ToString();
                }
                else
                    tempCode = o.ToString();

                TabModel tabs = new TabModel();
                tabs.Header = tabName;
                tabs.Code = tempCode;
                tabs.Content = (FrameworkElement)constructor.Invoke(null);
                TabModels.Add(tabs);
                TabIndex = TabModels.Count - 1;
            }
            this.MainContent = (FrameworkElement)constructor.Invoke(null);
        }
        private void getMenu()
        {
            string icon = string.Empty;
            ModuleGroups.Clear();
            MenuApi mApi = new MenuApi();

            var pageList = _repository.QueryList(m=>m.F_IsDisabled=="0").OrderBy(m => m.F_Sort).ToList();
            Menus = new ObservableCollection<MenuItemModel>();
            MenuHelper.FillMenus(Menus, null, pageList);
            var isAdd = false;
            if (Utils.IsAdmin)
            {
                foreach (var item in Menus)
                {                   
                    var Modules = new ObservableCollection<Modules>();
                    foreach (var arg in item.Children)
                    {
                        Modules.Add(new Modules()
                        {
                            Icon = arg.F_Icon,
                            Name = arg.F_Name,
                            TypeName = arg.F_TargetView,
                        });
                    }
                    ModuleGroups.Add(new ModuleGroup()
                    {
                        GroupName = item.F_Name,
                        Icon = item.F_Icon,
                        Modules = Modules,
                        ContractionTemplate = false,
                    });
                }
            }
            else
            {
                foreach (var item in Menus)
                {
                    isAdd = false;
                    var Modules = new ObservableCollection<Modules>();
                    foreach (var arg in item.Children)
                    {
                        var cnt = Utils.Auths.Where(m=>m.F_ModuleId==arg.F_Id).Count();//查询是否有权限
                        if(cnt> 0)
                        {
                            Modules.Add(new Modules()
                            {
                                Icon = arg.F_Icon,
                                Name = arg.F_Name,
                                TypeName = arg.F_TargetView,
                            });
                            isAdd = true;
                        }
                       
                    }
                    if(isAdd)
                    {
                        ModuleGroups.Add(new ModuleGroup()
                        {
                            GroupName = item.F_Name,
                            Icon = item.F_Icon,
                            Modules = Modules,
                            ContractionTemplate = false,
                        });
                    }
                   
                }
            }


            //Task.Run(new Action(async () =>
            //{
            //    var menu = await mApi.getGroup();
            //    ModuleGroups.Clear();
            //    foreach (var item in menu)
            //    {
            //        ModuleGroups.Add(item);
            //    }
            //}));
        }
    }
}
